<template>
  <div class="t-d-m">
    <div class="head">
      <div class="l">
        评论
      </div>
      <div class="r">最热 <van-icon name="descending" /></div>
    </div>
    <div class="body-item" v-for="i in discussList" :key="i.id">
      <div class="avatar">
        <van-image round :src="$host + i.avatar_thumb" />
      </div>
      <div class="right">
        <div class="top">
          <div class="name">
            <div>{{ i.user_nicename }}</div>
            <div class="time">{{ i.t }}</div>
          </div>
          <div class="btns">
            <span class="n">{{ i.up }}</span>
            <van-icon name="good-job-o" @click="upComment(i)" />
            <!-- <van-icon name="good-job-o" /><span class="n">{{ i.up }}</span>
            <van-icon name="good-job-o" class="no" /><span class="n">
              {{ i.down }}
            </span> -->
          </div>
        </div>
        <div class="comment-content">
          {{ i.comment }}
        </div>
        <!-- <div>
          <van-tag>回复</van-tag>
        </div> -->
      </div>
    </div>
    <div v-if="discussList.length == 0" class="no-discuss">
      啥也没有，快来评论下试试~
    </div>
    <div class="btm">
      <div class="lft">
        <van-field
          class="ipt"
          v-model="commentData.comment"
          placeholder="说点什么..."
          @focus="hideIpt(false)"
          @blur="hideIpt(true)"
        />
      </div>
      <div v-if="isInput" class="rht-btn">
        <van-button type="primary" @click="say" size="small">评论</van-button>
      </div>
      <div v-else class="rht">
        <div>
          <van-icon name="chat-o" class="icon" />
          <span class="num">{{ topic.comment }}</span>
        </div>
        <!-- <div>
          <van-icon name="good-job-o" class="icon" />
          <span class="num">{{ topic.comment }}</span>
        </div> -->
        <div @click="postCollect">
          <van-icon name="star-o" class="icon" />
          <span class="num">{{ topic.collect }}</span>
        </div>
        <div
          v-clipboard:copy="share"
          v-clipboard:success="onCopy"
          v-clipboard:error="onError"
        >
          <van-icon name="share" class="icon" />
        </div>
        <div @click="back">
          <van-icon name="arrow-left" class="icon" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
var timeago = require("timeago.js");
export default {
  name: "topic-discuss",
  props: ["topic"],
  data() {
    return {
      checked: true,
      apiData: {},
      isInput: false,
      commentData: {
        comment: ""
      },
      discussList: [],
      share: window.sessionStorage.getItem("share")
    };
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    onCopy(e) {
      this.$toast("已复制成功，快去分享吧~");
    },
    onError(e) {
      this.$toast("复制失败了~");
    },
    upComment(i) {
      this.$api.UpComment(i.id).then(r => {
        // console.log(r);
        this.$toast("赞一个~");
        i.up++;
      });
    },
    hideIpt(flag) {
      document.body.scrollTop = document.body.scrollHeight;
      if (flag) {
        if (!this.commentData.comment) {
          this.isInput = false;
        }
      } else {
        this.isInput = true;
      }
    },
    postCollect() {
      this.$api.PostCollect(this.topic.id).then(r => {
        // console.log(r);
        if (r.data.code == 0) {
          if (r.data.info.id) {
            this.$toast("收藏成功~");
            this.topic.collect++;
          } else {
            this.$toast("已取消收藏~");
            this.topic.collect--;
          }
        }
      });
    },
    getComment() {
      let _this = this;
      this.$api.getComment(this.topic.id).then(r => {
        this.discussList = r.data.info.list;
        this.discussList.map(e => {
          e.t = timeago.format(e.created_at, "zh_CN");
        });
        // console.log(this.discussList);
      });
    },
    say() {
      if (!this.commentData.comment) {
        this.$toast("请先说点什么~");
        return;
      }
      this.commentData.pid = this.topic.id;
      this.$api.postComment(this.commentData).then(r => {
        this.getComment();
        this.commentData = {};
      });
    }
  },
  mounted() {
    this.getComment();
  }
};
</script>

<style lang="less" scoped>
.btm {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  background: #fff;
  .lft {
    flex: 150px;
    padding: 5px;
    padding-left: 20px;
    .ipt {
      background: #f8f8fa !important;
      padding: 5px;
      border-radius: 5px;
    }
  }
  .lft-large {
    flex: 350px;
  }
  .rht-btn {
    padding: 6px;
  }
  .rht {
    flex: 150px;
    display: inline-flex;
    // padding: 0px 20px;
    padding-top: 10px;
    text-align: center;
    .icon {
      font-size: 24px;
      vertical-align: top;
    }
    .num {
      font-size: 10px;
      vertical-align: top;
    }
    div {
      flex: 25%;
    }
  }
}
.no-discuss {
  text-align: center;
  line-height: 100px;
}
.t-d-m {
  font-size: 16px;
  .head {
    padding: 10px 15px;
    font-size: 15px;
    // border-bottom: 1px solid #eee;
    display: flex;
    .l {
      flex: 200px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .van-icon {
        margin-left: 5px;
      }
    }
    .r {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      font-size: 14px;
      flex: 100px;
      .van-switch {
        margin-left: 5px;
      }
    }
  }
  .body-item {
    display: flex;
    padding: 10px 15px;
    padding-bottom: 20px;
    // border-bottom: 1px solid #eee;
    .avatar {
      flex: 40px;
      .van-image {
        width: 40px;
        height: 40px;
      }
    }
    .right {
      flex: 300px;
      .comment-content {
        margin-top: 5px;
      }
      .top {
        position: relative;
        .name {
          overflow: hidden;
          width: 200px;
          font-size: 14px;
          .time {
            font-size: 10px;
            color: #888;
          }
        }
        .btns {
          font-size: 18px;
          position: absolute;
          right: 0;
          top: 0;
          // .van-icon {
          //   margin: 0 10px;
          // }
          .no {
            transform: rotate(180deg);
          }
          .n {
            font-weight: 100;
            font-size: 10px;
            vertical-align: top;
          }
        }
      }
    }
  }
}
</style>
